<template>
  <o-modal
    v-model="isShow"
    :title="config.modalTitle"
    :size="config.modalSize || [560, 420]"
    :mask="true"
    :tap-close="false"
    :show-footer="true"
    @on-ok="transferOk"
    @on-close="transferClose"
  >
    <div slot="content" class="transferbox clearfix">
      <Transfer
        :not-found-text="$srv.getI18nByPath('others.listisempty')"
        :filter-placeholder="$srv.getI18nByPath('others.entersearch')"
        :data="getData"
        :target-keys="config.targetKeys"
        :render-format="renderFormat"
        :list-style="config.transferSize || { width: '225px', height: '290px' }"
        :filterable="config.filterable"
        :titles="config.transferTitle"
        @on-change="onChange"
        @on-selected-change="onSelectedChange"
      ></Transfer>
    </div>
  </o-modal>
</template>

<script lang="ts">
import { Component, Vue, Prop, Watch } from 'vue-property-decorator';
import oModal from '@/components/base/modal/o-modal/index.vue';
import { Tsrv, T_srv } from '../o-setup-panel/config';

interface data {
  key: number;
  label: string;
}
interface size {
  width: string;
  height: string;
}
interface config {
  data: Array<data>;
  targetKeys: Array<number>;
  filterable?: boolean;
  modalTitle?: string;
  modalSize?: Array<number>;
  transferTitle?: Array<string>;
  transferSize?: size;
}

@Component({
  components: {
    oModal
  }
})
export default class oTransfer extends Vue {
  /**
   * props
   */
  @Prop() config: config;

  @Prop() id: number;
  // @Prop() data: Array<data> = [];

  /**
   * data
   */
  private isShow: boolean = false;

  get getData() {
    const datas: Array<data> = [];
    this.config.data.forEach(el => {
      datas.push({
        key: el.key,
        label: Tsrv.getI18nBySrv(el.label)
      });
    });
    return datas;
  }

  created() {
    this.show();
  }

  mounted() {}

  public show() {
    this.isShow = true;
  }

  public hide() {
    this.isShow = false;
  }

  public transferOk() {
    this.$bus.$emit(`modal_${this.id}`, this.config.targetKeys);
    this.hide();
  }

  public transferClose() {
    this.$bus.$emit(`modal_${this.id}`);
    this.hide();
  }

  public renderFormat(item: any) {
    return item.label;
  }

  public onChange(
    newTargetKeys: number[],
    direction: string,
    moveKeys: number[]
  ) {
    this.config.targetKeys = newTargetKeys;
    // this.$bus.$emit("modal_" + this.id, newTargetKeys);
  }

  public onSelectedChange(
    sourceSelectedKeys: number[],
    targetSelectedKeys: number[]
  ) {
    // this.$emit("on-selected-change", sourceSelectedKeys, targetSelectedKeys);
  }
}
</script>

<style lang="scss" scoped>
.transferbox {
  padding: 0 20px;
  height: 100%;
}
</style>
